<template>
  <div>
    <!-- 上层面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="default" @click="showAddCatDialog"
          >添加分类</el-button
        >
      </div>

      <tree-table
        :data="categoriesList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
        show-index
        index-text="#"
        border
      >
        <!--是否有效Table -->
        <template slot="scope_1" slot-scope="scope">
          <i
            class="el-icon-error"
            v-if="scope.row.cat_deleted === false"
            style="color: red"
          ></i>
          <i class="el-icon-success" v-else style="color: green"></i>
        </template>

        <!--分层等级 -->
        <template slot="level" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="danger" v-else>三级</el-tag>
        </template>

        <!-- 操作 -->
        <template slot="opt">
          <el-button type="primary" size="default" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button type="danger" size="default" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页下边栏 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 4, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类Dialog -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCatDialogVisible"
      width="50%"
      @close="addCatDialogClosed"
    >
      <el-form
        :model="addCatForm"
        ref="addCatFormRef"
        :rules="addCatFormRules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <!-- 表单分类名称填写 -->
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCatForm.cat_name" @click="addCat"></el-input>
        </el-form-item>

        <!-- 表单多选框  -->

        <!-- Cascader 级联选择器 -->
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectedKey"
            :options="ParentCatList"
            :props="casaderProps"
            @change="parentCatChange"
            clearable
          ></el-cascader>
        </el-form-item>
        <!--    checkStrictly父子可以单独选择不受到强制关联 -->
        <!-- 添加和取消按钮 -->
        <el-form-item> </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCat">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categoriesList: [], //分类数据
      total: 30,
      addCatDialogVisible: false, //显示关闭添加分类数据Dialog
      addCatForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
      //添加分类数据表单验证规则
      addCatFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //table中get请求接受参数
      queryInfo: {
        type: 3, //接受到第几级数据
        pagenum: 1, //当前页码值
        pagesize: 5, //每页显示多少条数据
      },
      //自定义组件接受参数和渲染表格
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
          width: "300px",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          width: "300px",
          template: "scope_1", //提供slot-scope插槽 参数为插槽名 接受为slot-xxxxx
        },
        {
          label: "分类层级",
          prop: "cat_level",
          width: "300px",
          type: "template",
          template: "level",
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
        },
      ],

      //添加分类中的父级数据
      ParentCatList: [],

      // cascader用于渲染其数据:Options的参数
      casaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
      },

      selectedKey: "",
    };
  },
  created() {
    this.getCategoriesList();
  },
  methods: {
    // 获取商品分裂信息
    async getCategoriesList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类列表失败");
      }
      this.categoriesList = res.data.result;
      this.total = res.data.total;
      console.log(res);
      return this.$message.success("get获取商品分类列表成功");
    },

    // 分页栏事件
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getCategoriesList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getCategoriesList();
    },

    //显示添加分类Dialog
    showAddCatDialog() {
      this.getParentCatList();
      this.addCatDialogVisible = true;
    },

    //在添加分类显示栏中 获取父级分类的数据列表，获取前2级就可以
    async getParentCatList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类列表失败");
      }
      this.ParentCatList = res.data;
      return this.$message.success("get获取商品分类列表成功");
    },

    //选择项发生变化触发器函数
    parentCatChange() {
      //当length>0的时候，说明已经选中了 其中一个父级分类，我们请求了1级和2级分类，所以1级length=1  1->2级为length=2
      if (this.selectedKey.length > 0) {
        this.addCatForm.cat_level = this.selectedKey.length; //长度赋值给表单的cat_level则为正确的等级，因为第一级是0 二级是1 三级是2,根据length的长度就是父亲的下一级
        this.addCatForm.cat_pid = this.selectedKey[this.selectedKey.length - 1]; //获取插入分类中的父亲PID
        //  console.log(this.selectedKey[this.selectedKey.length-1]);

        console.log(this.selectedKey);
        console.log(this.addCatForm);
        return;
      } else {
        this.addCatForm.cat_level = 0;
        this.addCatForm.cat_pid = 0;
      }
    },
    addCat() {
      console.log(this.addCatForm);
      this.$refs.addCatFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "categories",
          this.addCatForm
        );
        if (res.meta.status != 201) {
          return this.$message.error("POST请求Categories添加分类失败");
        }
        this.getCategoriesList();
        this.addCatDialogVisible = false;
        return this.$message.success("请求添加分类成功");
      });
    },
    addCatDialogClosed() {
      this.$refs.addCatFormRef.resetFields();
      this.selectedKey = [];
      this.addCatForm.cat_pid = 0;
      this.addCatForm.cat_level = 0;
    },
  },
};
</script>


<style lang="less" scoped>
</style>